package icu.qx.pro.service.impl;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import icu.qx.common.constant.TableNameEnum;
import icu.qx.common.pojo.vo.IndexCondition;
import icu.qx.common.utils.CodeUtils;
import icu.qx.pro.domain.ProWorkOrder;
import icu.qx.pro.service.IProWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import icu.qx.pro.mapper.ProScheduleMapper;
import icu.qx.pro.domain.ProSchedule;
import icu.qx.pro.service.IProScheduleService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 生产计划Service业务层处理
 *
 * @author yong
 * @date 2024-06-20
 */
@Service
public class ProScheduleServiceImpl implements IProScheduleService {
    @Autowired
    private ProScheduleMapper proScheduleMapper;
    @Autowired
    @Lazy
    private IProWorkOrderService proWorkOrderService;
    @Autowired
    private CodeUtils codeUtils;

    /**
     * 查询生产计划
     *
     * @param scheduleCode 生产计划主键
     * @return 生产计划
     */
    @Override
    public ProSchedule selectProScheduleByScheduleCode(String scheduleCode) {
        return proScheduleMapper.selectProScheduleByScheduleCode(scheduleCode);
    }

    @Override
    public ProSchedule selectSimpleProScheduleByScheduleCode(String scheduleCode) {
        return proScheduleMapper.selectSimpleProScheduleByScheduleCode(scheduleCode);
    }

    /**
     * 查询生产计划列表
     *
     * @param proSchedule 生产计划
     * @return 生产计划
     */
    @Override
    public List<ProSchedule> selectProScheduleList(ProSchedule proSchedule) {
        return proScheduleMapper.selectProScheduleList(proSchedule);
    }

    @Override
    public List<IndexCondition> selectScheduleCondition() {
        return proScheduleMapper.selectScheduleCondition();
    }

    /**
     * 新增生产计划
     *
     * @param proSchedule 生产计划
     * @return 结果
     */
    @Override
    public int insertProSchedule(ProSchedule proSchedule) {
        String code = codeUtils.generateCode(TableNameEnum.PRO_SCHEDULE.getTableName());
        proSchedule.setScheduleCode(code);
        proSchedule.setCreateBy(SecurityUtils.getUsername());
        proSchedule.setCreateTime(DateUtils.getNowDate());
        return proScheduleMapper.insertProSchedule(proSchedule);
    }

    /**
     * 修改生产计划
     *
     * @param proSchedule 生产计划
     * @return 结果
     */
    @Override
    public int updateProSchedule(ProSchedule proSchedule) {
        proSchedule.setUpdateBy(SecurityUtils.getUsername());
        proSchedule.setUpdateTime(DateUtils.getNowDate());
        return proScheduleMapper.updateProSchedule(proSchedule);
    }

    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @Override
    public int decomposeProSchedule(List<ProWorkOrder> proWorkOrders) {
        ProSchedule proSchedule = new ProSchedule();
        proSchedule.setScheduleCode(proWorkOrders.get(0).getScheduleCode());
        proSchedule.setScheduleWorkNum((long) proWorkOrders.size());
        proSchedule.setScheduleStatus("1");
        // 删除
        proWorkOrderService.deleteProWorkOrderByScheduleCode(proSchedule.getScheduleCode());
        // 新增
        proWorkOrders.forEach(e -> {
            e.setWorkOrderCode(codeUtils.generateCode(TableNameEnum.PRO_WORK_ORDER.getTableName()));
            proWorkOrderService.insertProWorkOrder(e);
        });
        return updateProSchedule(proSchedule);
    }

    @Override
    public List<ProWorkOrder> selectRelatedOrders(String scheduleCode) {
        ProWorkOrder proWorkOrder = new ProWorkOrder();
        proWorkOrder.setScheduleCode(scheduleCode);
        proWorkOrder.setWorkOrderStatus("0");
        return proWorkOrderService.selectProWorkOrderList(proWorkOrder);
    }

    /**
     * 批量删除生产计划
     *
     * @param scheduleCodes 需要删除的生产计划主键
     * @return 结果
     */
    @Override
    public int deleteProScheduleByScheduleCodes(String[] scheduleCodes) {
        return proScheduleMapper.deleteProScheduleByScheduleCodes(scheduleCodes);
    }
}
